
import Flexbox from 'vux/src/components/flexbox/flexbox.vue'
import FlexboxItem from 'vux/src/components/flexbox/flexbox-item.vue'
import Checker from 'vux/src/components/checker/checker.vue'
import CheckerItem from 'vux/src/components/checker/checker-item.vue'
import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
export default {
  name: 'donation',
  components: {
    Flexbox, FlexboxItem, Checker, CheckerItem
  },
  data () {
    return {
      demo: '不设金额',
      textarea: '',
      isgeren: true,
      istuanti: false,
      isqiye: false,
      style: 'personal',
      companyName: '',
      // 选择的哪一个发起形式
      isWitch_: 'geren',
      event_id: '',
      userId: '',
      together_id: '',
      list: {
        content: '',
        eventName: '',
        eventPicUrl: '',
        raiseMoney: ''
      },
      // 目标金额
      goalAmount: ''
    }
  },
  mounted () {
    if (this.$emit('ifOpenid')) {
      this.$emit('ifOpenid', this.$route.path)
    }
    if (this.$emit('ShareAppMessage')) {
      this.$emit('ShareAppMessage', '亲青筹', '“亲青筹”由浙江团省委、青基会发起设立，通过众筹善款，服务青少年成长。', 'http://www.qqchou.org/a/wxinterface/wxOAuth', this.imageUrl.imageUrl+'/userfiles/2/images/1513911875.png')
    }
    this.userId = window.sessionStorage.getItem('userId')
    this.event_id = this.$route.query.event_id;
    http.post(CONSTANT.TOGETHER_DONATE, {event_id: this.event_id}, (res) => {
      if (res.code == 200) {
        console.log(res)
        this.list = res.obj
      }
    })
  },
  methods: {
    comeback () {
      window.history.go(-1)
    },
    goHomedonnation () {
      // 点击下一步时触发
      let num = /^[1-9](([0-9]+\.[0-9]{2})|([0-9]+\.[0-9]{1})|([0-9]*))$/
      if (this.isWitch_ == '') {
        alert('请选择一种发起形式！')
      } else if (this.isWitch_ == 'tuanti'  && this.companyName == '') {
         alert('请填写团体名称！');
      } else if (this.isWitch_ == 'qiye' && this.companyName == '') {
        alert('请填写企业名称！');
      } else if (this.textarea == '') {
        alert('请填写发起说明！')
      } else if (this.demo == '') {
        alert('请选择筹款目标！')
      } else if (this.demo == '其它金额' && this.goalAmount == '') {
        alert('请输入目标金额！')
      } else if (!num.test(this.goalAmount) && this.demo == '其它金额') {
        alert('请输入正确的金额数!（提示：只能是数字或者精确到两位小数）')
      } else if (this.goalAmount == 0 && this.demo == '其它金额' || this.goalAmount == '0.00' && this.demo == '其它金额') {
        alert('请输入正确的金额数!（提示：只能是数字或者精确到两位小数）')
      } else {
        console.log(this.companyName)
        if (this.demo == '不设金额') {
          this.goalAmount = ''
        }
        http.post(CONSTANT.ACHIEVE_INVITATION, {
          userId: this.userId,
          event_id: this.event_id,
          style: this.style,
          content: this.textarea,
          goalAmount: this.goalAmount,
          name: this.companyName
        }, (res) => {
          console.log(res)
          if (res.code == 200) {
            window.sessionStorage.setItem('together_id', res.obj.together_id)
            this.together_id = res.obj.together_id
            this.$router.push({path: '/homedonation', query: {together_id: this.together_id}})
          } else {
            console.log('出错了')
          }
        })
      }
    },
    checkerWitch () {
      // 选择筹款目标是触发
    },
    isWitch (e) {
      // 选择发起形式时触发
      let _this = this
      _this.isWitch_ = e
      if (e == 'geren') {
        _this.isgeren = true
        _this.istuanti = false
        _this.isqiye = false
        _this.style = 'personal'
      } else if (e == 'tuanti') {
        _this.isgeren = false
        _this.istuanti = true
        _this.isqiye = false
        _this.style = 'group'
      } else if (e == 'qiye') {
        _this.isgeren = false
        _this.istuanti = false
        _this.isqiye = true
        _this.style = 'company'
      }
    }
  }
}
